package com.ruitu.health_1.ui.fragment.order;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.google.gson.Gson;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_1.R;
import com.ruitu.health_1.dao.bean.GoodsInfo;
import com.ruitu.health_1.dao.bean.OrderItemList;
import com.ruitu.health_1.dao.bean.OrderList;
import com.ruitu.health_1.dao.bean.StoreOrderItemList;
import com.ruitu.health_1.mvp.model.OrderModel;
import com.ruitu.health_1.mvp.presenter.OrderPresenter;
import com.ruitu.health_1.ui.OrderDetailsActivity;
import com.ruitu.health_1.ui.ShippedActivity;
import com.ruitu.health_1.util.CustomDialogUtil;
import com.ruitu.health_1.util.CustomInterface;
import com.ruitu.health_1.view.NoCacheLoadingFragment;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.util.ReqUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WaitingSendFragment extends NoCacheLoadingFragment<OrderPresenter.OrderP, OrderModel.OrderM> {
    public static WaitingSendFragment newInstance() {

        Bundle args = new Bundle();

        WaitingSendFragment fragment = new WaitingSendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_order_list;
    }

    private List<Integer> skipId;

    @Override
    protected void initViews(View v) {
        skipId = new ArrayList<>();
        showLoadingView(skipId);
        getDataList();
    }

    private void getDataList() {
        p.orderList(1, size, AppHolder.instance().getUser().getId(), "2");
    }

    private StoreDataAdapter adapter;

    @Override
    protected ProgressLayout findProgressLayout(View v) {
        return null;
    }

    @Override
    protected LRecyclerView findRecyclerView(View v) {
        return v.findViewById(R.id.l_recyclerview);
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new StoreDataAdapter(getContext());
        return adapter;
    }

    @Override
    protected void reqFirst() {
        size = 10;
        getDataList();
    }

    private int size = 10;

    @Override
    protected void reqMore() {
        size = size + 10;
        getDataList();
    }

    @Override
    public void setData(List<StoreOrderItemList> orderLists) {
        showContent(skipId);
        adapter.setDataList(orderLists);
    }

    @Override
    public void confirmReceipt() {
        super.confirmReceipt();
        getDataList();
    }

    class DataAdapter extends ListBaseAdapter<OrderList> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_order_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            final OrderList orderList = mDataList.get(position);
            RecyclerView recyclerview = holder.getView(R.id.recyclerview);
            View view = holder.getView(R.id.divider);

            view.setVisibility(position > 0 ? View.VISIBLE : View.GONE);

            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            recyclerview.setLayoutManager(manager);
            manager.setOrientation(RecyclerView.VERTICAL);
            ArrayList<StoreOrderItemList> storeOrderItemList = orderList.getStoreOrderItemList();
            for (int i = 0; i < storeOrderItemList.size(); i++) {
                storeOrderItemList.get(i).setOrderStatus(orderList.getOrder().getStatus());
            }
            StoreDataAdapter childDataAdapter = new StoreDataAdapter(mContext);
            recyclerview.setAdapter(childDataAdapter);
            childDataAdapter.setDataList(storeOrderItemList);

        }
    }

    private ArrayList idList;
    private CustomDialogUtil dialogUtil;

    class StoreDataAdapter extends ListBaseAdapter<StoreOrderItemList> {
        public StoreDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_store_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            LinearLayout ll_wait_send = holder.getView(R.id.ll_wait_send);
            LinearLayout ll_shipped = holder.getView(R.id.ll_shipped);
            RecyclerView recyclerview = holder.getView(R.id.l_recyclerview);
            TextView tv_store_name = holder.getView(R.id.tv_store_name);
            TextView tv_send_detail = holder.getView(R.id.tv_send_detail);
            TextView tv_send_cancel = holder.getView(R.id.tv_send_cancel);
            TextView tv_check_logistics = holder.getView(R.id.tv_check_logistics);
            TextView tv_confirm_receipt = holder.getView(R.id.tv_confirm_receipt);
            final StoreOrderItemList orderItemList = mDataList.get(position);
            tv_store_name.setText(orderItemList.getName());
            if (TextUtils.equals("1", orderItemList.getOrder().getStatus())) {
                ll_wait_send.setVisibility(View.VISIBLE);
                ll_shipped.setVisibility(View.GONE);
            } else {
                ll_wait_send.setVisibility(View.GONE);
                ll_shipped.setVisibility(View.VISIBLE);
            }
            ChildDataAdapter childDataAdapter = new ChildDataAdapter(mContext);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);

            recyclerview.setLayoutManager(manager);
            recyclerview.setAdapter(childDataAdapter);
            childDataAdapter.setDataList(orderItemList.getOrderItemList());

            tv_send_detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("orderId", new Gson().toJson(ids(orderItemList)));
                    startActivity(OrderDetailsActivity.class, bundle);

                }
            });

            //取消订单
            tv_send_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            //查看物流
            tv_check_logistics.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtils.showShort("暂无物流信息");
                }
            });

            //确认收货
            tv_confirm_receipt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (orderItemList.getOrderItemList().size() <= 1) {
                        String orderIds = new Gson().toJson(ids(orderItemList));
                        final Map map = new HashMap<>();
                        map.put("ids", orderIds);
                        map.put("memberId", AppHolder.instance().getUser().getId());
                        //单个订单的处理
                        dialogUtil = new CustomDialogUtil(getContext(), "您是否收到该订单商品?", "已收货", "未收货", new CustomInterface.OnBtnClickListener() {
                            @Override
                            public void onLeftClick() {
                                showProgress();
                                p.confirmReceipt(ReqUtil.getBody(map, true));
                            }

                            @Override
                            public void onRightClick() {
                                dialogUtil.dismiss();
                            }
                        });
                        dialogUtil.show();
                        return;
                    }
                    String orderIds = new Gson().toJson(ids(orderItemList));
                    final Map map = new HashMap<>();
                    map.put("ids", orderIds);
                    map.put("memberId", AppHolder.instance().getUser().getId());
                    //单个订单的处理
                    //选择确认收货
                    dialogUtil = new CustomDialogUtil(getContext(), "请选择确认收货方式", "全部确认", "单选确认"
                            , new CustomInterface.OnBtnClickListener() {
                        @Override
                        public void onLeftClick() {

                            showProgress();
                            p.confirmReceipt(ReqUtil.getBody(map, true));
                        }

                        @Override
                        public void onRightClick() {
                            Bundle bundle = new Bundle();
                            bundle.putParcelableArrayList("order", orderItemList.getOrderItemList());
                            startActivity(ShippedActivity.class, bundle);
                        }
                    });
                    dialogUtil.show();


                }
            });
        }
    }

    class ChildDataAdapter extends ListBaseAdapter<OrderItemList> {

        public ChildDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_child_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);
            OrderItemList listBean = mDataList.get(position);
            Glide.with(mContext).load(listBean.getProductPic()).apply(Arad.opts).into(iv_commodity);

            tv_commodity_name.setText(listBean.getProductName());
//            tv_commodity_num.setText(listBean.getRealAmount());
            tv_commodity_price.setText(listBean.getRealAmount());
            StringBuffer buffer = new StringBuffer();
            GoodsInfo good = listBean.getGood();
            if (good != null) {
                if (!TextUtils.isEmpty(good.getSp1())) {
                    buffer.append(good.getSp1());
                }
                if (!TextUtils.isEmpty(good.getSp2())) {
                    buffer.append("," + good.getSp2());
                }
                if (!TextUtils.isEmpty(good.getSp3())) {
                    buffer.append("," + good.getSp3());
                }
            }
            tv_commodity_info.setText(buffer.toString());
        }
    }

    //获取所有确认收货订单的id
    private ArrayList<String> ids(StoreOrderItemList storeOrderItemList) {
        idList = new ArrayList<>();
        List<OrderItemList> orderItemList = storeOrderItemList.getOrderItemList();
        for (int i = 0; i < orderItemList.size(); i++) {
            idList.add(orderItemList.get(i).getId());
        }
        return idList;
    }
}
